//
//  CustomControl.m
//  U9WatchGame
//
//  Created by uuu9 on 16/3/17.
//  Copyright © 2016年 Jason. All rights reserved.
//

#import "CustomControl.h"

#define sizeHeight 6

@interface CustomControl ()
@property (nonatomic, strong) UIImage* activeImage;
@property (nonatomic, strong) UIImage* inactiveImage;

@end

@implementation CustomControl

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.activeImage = [UIImage imageNamed:@"d8"];
        self.inactiveImage = [UIImage imageNamed:@"d8s"];
        self.userInteractionEnabled = NO;
        self.backgroundColor = [UIColor blackColor];
        self.alpha = 0.7;
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
    }
    return self;
}

- (void)setNumberOfPages:(NSInteger)pages{
    for (int i = 0; i < pages; i++) {
        UIImageView *pointImageView = [[UIImageView alloc] initWithFrame:CGRectMake(4  + 10 *i, (10 - sizeHeight ) / 2, sizeHeight, sizeHeight)];
        pointImageView.layer.masksToBounds = YES;
        pointImageView.layer.cornerRadius = 4;
        pointImageView.contentMode = UIViewContentModeScaleToFill;
        [self addSubview:pointImageView];
    }
}


-(void)updateDots
{
    for (int i = 0; i<[self.subviews count]; i++) {
        UIImageView *dot = [self.subviews objectAtIndex:i];
        CGSize size = CGSizeMake(sizeHeight, sizeHeight);
        [dot setFrame:CGRectMake(dot.frame.origin.x, dot.frame.origin.y, size.width, size.width)];
        if (i == self.myCurrentPage) {
            dot.image = self.activeImage;
        } else {
            dot.image = self.inactiveImage;
        }
    }
}


- (void)setCurrentPage:(NSInteger)page
{
    self.myCurrentPage = page;
    [self updateDots];
}

@end
